library(ggplot2)   # devtools::install_github("hadley/ggplot2") only if you want subtitles/captions
library(foreign)
library(stringr)
library(dplyr)
library(tidyr)
library(rgeos)
library(maptools)
library(ggalt)
library(ggthemes)
library(albersusa) # devtools::install_github("hrbrmstr/albersusa")
library(viridis)
library(scales)

Prepping the data

counties <- read.spss("data/religions_counties.SAV", 
                      to.data.frame = TRUE)

counties$CNTYNAME <- as.character(counties$CNTYNAME)
counties$CNTYNAME <- sub("County", "", counties$CNTYNAME)
counties$CNTYNAME <- sub("\\s+$", "", counties$CNTYNAME)

counties_rates <- counties[ , grepl( "RATE" , names( counties ) ) ]


cmap <- fortify(counties_composite(), region="fips")
cmap$id <- as.numeric(cmap$id)
counties[is.na(counties)] <- 0

Function to generate a map

religion_map <- function(counties, religion, religion_id) {
  
  headline <- ifelse(grepl("RATE", as.character(religion_id)), paste("Rate of", religion, "followers"), paste(religion, "followers"))
  legend <- ifelse(grepl("RATE", as.character(religion_id)), paste(religion, "per 1,000 residents"), "Followers")
  
  counties_sub <- counties
  
  names(counties_sub)[names(counties_sub) == religion_id] <- 'for_map'
  
  gg <- ggplot()
  gg <- gg + geom_map(data=cmap, map=cmap,
                      aes(x=long, y=lat,map_id=id),
                      color="#2b2b2b", size=0.05, fill=NA)
  gg <- gg + geom_map(data=counties_sub, map=cmap,
                      aes(fill=for_map, map_id=FIPS),
                      color="#2b2b2b", size=0.05)
  gg <- gg + scale_fill_viridis(name=legend)
  gg <- gg + coord_proj(us_laea_proj)
  gg <- gg + labs(title=headline,
                  caption="TrendCT.org\nSOURCE: Association of Religion Data Archives 2010")
  gg <- gg + theme_map(base_family="Arial Narrow")
  gg <- gg + theme(legend.position=c(0.8, 0.25))
  gg <- gg + theme(plot.title=element_text(face="bold", size=14, margin=margin(b=6)))
  gg <- gg + theme(plot.subtitle=element_text(size=10, margin=margin(b=-14)))
  
  gg
}
religion_map(counties, "Evangelicals", "EVANRATE")
## Warning: Ignoring unknown aesthetics: x, y
## Warning: `panel.margin` is deprecated. Please use `panel.spacing` property
## instead

# congregations: CTHCNG
# adherants: CTHADH
religion_map(counties, "Catholicism", "CTHRATE")
## Warning: Ignoring unknown aesthetics: x, y
## Warning: `panel.margin` is deprecated. Please use `panel.spacing` property
## instead

# Mainline protestant
religion_map(counties, "Mainline Protestant", "MPRTRATE")
## Warning: Ignoring unknown aesthetics: x, y
## Warning: `panel.margin` is deprecated. Please use `panel.spacing` property
## instead

religion_map(counties, "Episcopal", "ECRATE")
## Warning: Ignoring unknown aesthetics: x, y
## Warning: `panel.margin` is deprecated. Please use `panel.spacing` property
## instead

# United methodist church
religion_map(counties, "Methodist", "UMCRATE")
## Warning: Ignoring unknown aesthetics: x, y
## Warning: `panel.margin` is deprecated. Please use `panel.spacing` property
## instead

# American Baptist
religion_map(counties, "American Baptist", "ABCRATE")
## Warning: Ignoring unknown aesthetics: x, y
## Warning: `panel.margin` is deprecated. Please use `panel.spacing` property
## instead

religion_map(counties, "Southern Baptist", "SBCRATE")
## Warning: Ignoring unknown aesthetics: x, y
## Warning: `panel.margin` is deprecated. Please use `panel.spacing` property
## instead

# Evangelical Lutheran Church
religion_map(counties, "Lutheran", "ELCARATE")
## Warning: Ignoring unknown aesthetics: x, y
## Warning: `panel.margin` is deprecated. Please use `panel.spacing` property
## instead

religion_map(counties, "Reform Judaism", "RFRMRATE")
## Warning: Ignoring unknown aesthetics: x, y
## Warning: `panel.margin` is deprecated. Please use `panel.spacing` property
## instead

religion_map(counties, "Conservative Judaism", "CJUDRATE")
## Warning: Ignoring unknown aesthetics: x, y
## Warning: `panel.margin` is deprecated. Please use `panel.spacing` property
## instead

religion_map(counties, "Muslim", "MSLMRATE")
## Warning: Ignoring unknown aesthetics: x, y
## Warning: `panel.margin` is deprecated. Please use `panel.spacing` property
## instead

religion_map(counties, "Greek Orthodox", "GRKRATE")
## Warning: Ignoring unknown aesthetics: x, y
## Warning: `panel.margin` is deprecated. Please use `panel.spacing` property
## instead

religion_map(counties, "Buddhism", "BUDMRATE")
## Warning: Ignoring unknown aesthetics: x, y
## Warning: `panel.margin` is deprecated. Please use `panel.spacing` property
## instead